package hjw.com.service;

import hjw.com.entity.User;
import hjw.com.dao.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>Project:Academic - UserMapperImpl
 *
 * @author IvanWei玮 -- maste
 * @version 1.0
 * @DataTime 2025/1/20 15:31
 * @since 1.8
 */
@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    private final PasswordEncoder passwordEncoder;

    @Autowired(required = false)
    public UserServiceImpl(UserMapper userMapper, PasswordEncoder passwordEncoder) {
        this.userMapper = userMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public User findOneByAccount(String account) {
        return userMapper.findOneByAccount(account);
    }

    @Override
    public int register(Map<String, Object> map) {
        String password = (String) map.get("password");
        //初始化未提交的注册密码
        if (password == null || password.isEmpty()){
            map.put("password", passwordEncoder.encode("123456"));
        }
        //加密修改或注册的密码
        if (password != null && !password.isEmpty()){
            map.put("password", passwordEncoder.encode(password));
        }
        return userMapper.register(map);
    }

    @Override
    public int update(Map<String, Object> map) {
//        String password = (String) map.get("password");
//        //初始化未提交的注册密码
//        if (password == null || password.isEmpty()){
//            map.put("password", passwordEncoder.encode("123456"));
//        }
        //加密修改或注册的密码
        String password = (String) map.get("password");
        if (password != null && !password.isEmpty()){
            map.put("password", passwordEncoder.encode(password));
        }
        return userMapper.update(map);
    }
}
